/*
 * @Description:
 * @Author: Junhuang
 * @Date: 2022-05-29 23:30:12
 * @LastEditTime: 2022-05-30 00:11:44
 * @LastEditors: Junhuang
 * @Reference:
 */
'use strict';

const Controller = require('egg').Controller;

class RankController extends Controller {
  async index() {
    const { ctx, app } = this;

    let rank1 = await app.model.Rank.findAll({
      include: [{
        model: app.model.User,
        attributes: [ 'nickname', 'description' ],
      }],
      order: [[ 'score', 'DESC' ], [ 'on_time_rate', 'DESC' ], [ 'user', 'nickname', 'ASC' ]],
      limit: 10,
    });

    rank1 = JSON.parse(JSON.stringify(rank1));
    let index = 0;
    rank1.forEach(element => {
      index++;
      element.key = index;
      element.num = element.score;
    });

    let rank2 = await app.model.Rank.findAll({
      include: [{
        model: app.model.User,
        attributes: [ 'nickname', 'description' ],
      }],
      order: [[ 'on_time_rate', 'DESC' ], [ 'score', 'DESC' ], [ 'user', 'nickname', 'ASC' ]],
      limit: 10,
    });

    rank2 = JSON.parse(JSON.stringify(rank2));
    index = 0;
    rank2.forEach(element => {
      index++;
      element.key = index;
      element.num = element.on_time_rate;
    });

    return ctx.apiSuccesss({ rank1, rank2 });
  }
}

module.exports = RankController;
